﻿namespace HySoft.Cash.Business
{
    using Huayou.Common.Database;
    using Huayou.Utils;
    using HySoft.Cash.BaseClass;
    using HySoft.Cash.DB;
    using HySoft.Cash.Structure;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Data.SqlClient;
    using System.Configuration;

    public class TorginfoBN : BaseBusiness
    {
        private TorginfoDB db;

        public TorginfoBN(Page page)
            : base(page)
        {
            this.db = null;
            this.db = new TorginfoDB(base.curDBOperater);
        }

        public TorginfoBN(Page page, DBOperate oper)
            : base(page, oper)
        {
            this.db = null;
            this.db = new TorginfoDB(base.curDBOperater);
        }

        /// <summary>
        /// 获取企业的状态文本
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetOrgStatusText(OrginfoStatuses status)
        {
            string statusText = string.Empty;
            switch (status)
            {
                case OrginfoStatuses.Normal:
                    statusText = "Normal";
                    break;
                case OrginfoStatuses.UnNormal:
                default:
                    statusText = "UnNormal";
                    break;
            }
            return statusText;
        }

        //
        public static string GetOrgKnownText(OrgInfoKnowns status)
        {
            string statusText = string.Empty;
            switch (status)
            {
                case OrgInfoKnowns.Known:
                    statusText = "Known";
                    break;
                case OrgInfoKnowns.UnKnown:
                default:
                    statusText = "UnKnown";
                    break;
            }
            return statusText;
        }

        public static double GetClosingBalance(int OrgSid)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@OrgSid", OrgSid) };
            DataTable table = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "GetCreditBalanceByOrgSid", commandParameters).Tables[0];
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0][0] == DBNull.Value)
                {
                    return 0.0;
                }
                else
                {
                    return Convert.ToDouble(table.Rows[0][0]);
                }
            }
            return 0.0;
        }

        /// <summary>
        /// 获取实时的CreditBalance，（包括尚未生成Invoice的Payment消耗的额度）
        /// </summary>
        /// <param name="OrgSid"></param>
        /// <returns></returns>
        public static double GetRealTimeCreditBalance(int OrgSid)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@OrgSid", OrgSid) };
            DataTable table = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "GetRealTimeCreditBalanceByOrgSid", commandParameters).Tables[0];
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0][0] == DBNull.Value)
                {
                    return 0.0;
                }
                else
                {
                    return Convert.ToDouble(table.Rows[0][0]);
                }
            }
            return 0.0;
        }

        public static TinvoicelistDT GetLatestInvoice(int OrgSid)
        {

            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@OrgSid", OrgSid) };
            DataTable table = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "GetLatestInvoiceByOrg", commandParameters).Tables[0];
            if (table.Rows.Count > 0)
            {
                DataRow firstRow = table.Rows[0];
                TinvoicelistDT invoiceDT = TinvoicelistDT.ConvertFrom(firstRow);
                //invoiceDT.Guid = firstRow["Guid"].ToString();
                //invoiceDT.Sid = Convert.ToInt32(firstRow["Sid"]);
                //invoiceDT.InvoiceDate = Convert.ToDateTime(firstRow["InvoiceDate"]);
                //invoiceDT.LateInterestCharges = Convert.ToDecimal(firstRow["LateInterestCharges"]);
                //invoiceDT.InvoiceAmount = Convert.ToDecimal(firstRow["InvoiceAmount"]);
                //invoiceDT.IsValid = (InvoiceStatuses)Convert.ToInt32(firstRow["IsValid"]);
                //invoiceDT.Note = Convert.ToString(firstRow["Note"]);
                //invoiceDT.OrgSid = Convert.ToInt32(firstRow["OrgSid"]);
                //if (Convert.IsDBNull( firstRow["PaymentDate"]) ==false)
                //{
                //    invoiceDT.PaymentDate = Convert.ToDateTime(firstRow["PaymentDate"]);
                //}
                //invoiceDT.Penalty = Convert.ToDecimal(firstRow["Penalty"]);
                //invoiceDT.ReceiveAmount = Convert.ToDecimal(firstRow["ReceiveAmount"]);
                return invoiceDT;
            }
            else
            {
                return null;
            }
        }

        public bool Add(TorginfoDT detail)
        {
            bool flag = false;
            try
            {
                try
                {
                    flag = this.db.Add(detail);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return flag;
        }

        public int AddId(TorginfoDT detail)
        {
            int num = 0;
            try
            {
                try
                {
                    num = this.db.AddId(detail);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return num;
        }

        public bool Delete(int Sid)
        {
            bool flag = false;
            try
            {
                try
                {
                    flag = this.db.Delete(Sid);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return flag;
        }

        public void Dispose()
        {
            base.Dispose();
            if (this.db != null)
            {
                this.db = null;
            }
        }

        /// <summary>
        /// 其不可以修改还款的余额RemainBalance
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        public bool Edit(TorginfoDT detail)
        {
            bool flag = false;
            try
            {
                try
                {
                    flag = this.db.Edit(detail);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return flag;
        }

        /// <summary>
        /// 修正用户还款的余额
        /// </summary>
        /// <param name="remainBalanceAdded">新增加的余额</param>
        /// <returns></returns>
        public bool ModifyRemainBalance(int orgID, decimal remainBalanceAdded)
        {
            bool flag = false;
            try
            {
                flag = this.db.ModifyRemainBalance(orgID, remainBalanceAdded);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return flag;
        }

        public TorginfoDT Get(int Sid)
        {
            TorginfoDT odt = null;
            try
            {
                try
                {
                    odt = this.db.Get(Sid);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return odt;
        }

        public DataTable GetList()
        {
            base.DBFieldList = TorginfoDT.allFields; //"Sid,AgreementDate,OrgName,TradingName,ABN,Industry,OfficeAddress,WebSite,ContactPerson,sPosition,ExpCreditLimit,PostalAddress,ContactPhone1,ContactPhone2,Fax,Mobile,PreferredEmail,AltEmail1,AltEmail2,UserName,PassWord,RegDate,IsValid,CreditLimit,CurrentBalance,AvailableBalance";
            base.DBTable = "tOrgInfo";
            return base.CommonGetList();
        }

        public DataTable GetIDList()
        {
            base.DBFieldList = "Sid";
            base.DBTable = "tOrgInfo";
            return base.CommonGetList();
        }

        public void QueryABN(string ABN)
        {
            if ((ABN != null) && (ABN.Trim() != ""))
            {
                base.Filter = "ABN like '%" + StringUtils.ToSQL(ABN) + "%'";
            }
        }

        public void QueryAgreementDate(string AgreementDate)
        {
            if ((AgreementDate != null) && (AgreementDate.Trim() != ""))
            {
                base.Filter = "AgreementDate like '%" + StringUtils.ToSQL(AgreementDate) + "%'";
            }
        }

        public void QueryAltEmail1(string AltEmail1)
        {
            if ((AltEmail1 != null) && (AltEmail1.Trim() != ""))
            {
                base.Filter = "AltEmail1 like '%" + StringUtils.ToSQL(AltEmail1) + "%'";
            }
        }

        public void QueryAltEmail2(string AltEmail2)
        {
            if ((AltEmail2 != null) && (AltEmail2.Trim() != ""))
            {
                base.Filter = "AltEmail2 like '%" + StringUtils.ToSQL(AltEmail2) + "%'";
            }
        }

        //public void QueryAvailableBalance(decimal AvailableBalance)
        //{
        //    base.Filter = "AvailableBalance= " + AvailableBalance;
        //}

        public void QueryContactPerson(string ContactPerson)
        {
            if ((ContactPerson != null) && (ContactPerson.Trim() != ""))
            {
                base.Filter = "ContactPerson like '%" + StringUtils.ToSQL(ContactPerson) + "%'";
            }
        }

        public void QueryContactPhone1(string ContactPhone1)
        {
            if ((ContactPhone1 != null) && (ContactPhone1.Trim() != ""))
            {
                base.Filter = "ContactPhone1 like '%" + StringUtils.ToSQL(ContactPhone1) + "%'";
            }
        }

        public void QueryContactPhone2(string ContactPhone2)
        {
            if ((ContactPhone2 != null) && (ContactPhone2.Trim() != ""))
            {
                base.Filter = "ContactPhone2 like '%" + StringUtils.ToSQL(ContactPhone2) + "%'";
            }
        }

        public void QueryCreditLimit(decimal CreditLimit)
        {
            base.Filter = "CreditLimit= " + CreditLimit;
        }

        public void QueryCurrentBalance(decimal CurrentBalance)
        {
            base.Filter = "FeeBalance= " + CurrentBalance;
        }

        public void QueryExpCreditLimit(decimal ExpCreditLimit)
        {
            base.Filter = "ExpCreditLimit= " + ExpCreditLimit;
        }

        public void QueryFax(string Fax)
        {
            if ((Fax != null) && (Fax.Trim() != ""))
            {
                base.Filter = "Fax like '%" + StringUtils.ToSQL(Fax) + "%'";
            }
        }

        public void QueryIndustry(string Industry)
        {
            if ((Industry != null) && (Industry.Trim() != ""))
            {
                base.Filter = "Industry like '%" + StringUtils.ToSQL(Industry) + "%'";
            }
        }

        public void QueryIsValid(int IsValid)
        {
            base.Filter = "IsValid= " + IsValid;
        }

        public void QueryMobile(string Mobile)
        {
            if ((Mobile != null) && (Mobile.Trim() != ""))
            {
                base.Filter = "Mobile like '%" + StringUtils.ToSQL(Mobile) + "%'";
            }
        }

        public void QueryOfficeAddress(string OfficeAddress)
        {
            if ((OfficeAddress != null) && (OfficeAddress.Trim() != ""))
            {
                base.Filter = "OfficeAddress like '%" + StringUtils.ToSQL(OfficeAddress) + "%'";
            }
        }

        public void QueryOrgName(string OrgName)
        {
            if ((OrgName != null) && (OrgName.Trim() != ""))
            {
                base.Filter = "OrgName like '%" + StringUtils.ToSQL(OrgName) + "%'";
            }
        }

        public void QueryPassWord(string PassWord)
        {
            if ((PassWord != null) && (PassWord.Trim() != ""))
            {
                base.Filter = "PassWord like '%" + StringUtils.ToSQL(PassWord) + "%'";
            }
        }

        public void QueryPostalAddress(string PostalAddress)
        {
            if ((PostalAddress != null) && (PostalAddress.Trim() != ""))
            {
                base.Filter = "PostalAddress like '%" + StringUtils.ToSQL(PostalAddress) + "%'";
            }
        }

        public void QueryPreferredEmail(string PreferredEmail)
        {
            if ((PreferredEmail != null) && (PreferredEmail.Trim() != ""))
            {
                base.Filter = "PreferredEmail like '%" + StringUtils.ToSQL(PreferredEmail) + "%'";
            }
        }

        public void QueryRegDate(string RegDate)
        {
            if ((RegDate != null) && (RegDate.Trim() != ""))
            {
                base.Filter = "RegDate like '%" + StringUtils.ToSQL(RegDate) + "%'";
            }
        }

        public void QuerySid(int Sid)
        {
            base.Filter = "Sid= " + Sid;
        }

        public void QuerysPosition(string sPosition)
        {
            if ((sPosition != null) && (sPosition.Trim() != ""))
            {
                base.Filter = "sPosition like '%" + StringUtils.ToSQL(sPosition) + "%'";
            }
        }

        public void QueryTradingName(string TradingName)
        {
            if ((TradingName != null) && (TradingName.Trim() != ""))
            {
                base.Filter = "TradingName like '%" + StringUtils.ToSQL(TradingName) + "%'";
            }
        }

        public void QueryUserName(string UserName)
        {
            if ((UserName != null) && (UserName.Trim() != ""))
            {
                base.Filter = "UserName like '%" + StringUtils.ToSQL(UserName) + "%'";
            }
        }

        public void QueryWebSite(string WebSite)
        {
            if ((WebSite != null) && (WebSite.Trim() != ""))
            {
                base.Filter = "WebSite like '%" + StringUtils.ToSQL(WebSite) + "%'";
            }
        }
    }
}

